package com.zr.task.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zh.baselibrary.base.BaseActivity;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.constant.ExtraName;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.utils.JsonUtils;
import com.zh.baselibrary.utils.MD5;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.task.home.CabinetBean;
import com.zh.model.task.home.ClassCabinetBean;
import com.zr.task.R;
import com.zr.task.utils.ScreenUtils;
import com.zr.task.utils.ValidateUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnTextChanged;

public class TeacherLoginActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.login)
    Button loginBtn;
    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.scroll)
    LinearLayout scroll;
    List<CabinetBean> cabinetDetailList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_teacher_login;
    }

    @Override
    protected void init() {
        ScreenUtils.addLayoutListener(layout, scroll);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @OnFocusChange({R.id.et_phone, R.id.et_pwd})
    public void onFocusChange(View v, boolean hasFocus) {
        int drawableID = 0;
        if (v.getId() == R.id.et_pwd) {
            drawableID = hasFocus ? R.drawable.password_teacher_acitve : R.drawable.password_teacher;
        } else {
            drawableID = hasFocus ? R.drawable.iphone_active : R.drawable.iphone;
        }
        ((EditText) v).setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(drawableID),
                null, null, null);

    }

    @OnTextChanged({R.id.et_phone, R.id.et_pwd})
    public void onTextChanged(CharSequence text, int start, int before, int count) {
        if (TextUtils.isEmpty(mEtPhone.getText().toString()) || TextUtils.isEmpty(mEtPwd.getText().toString()))
            loginBtn.setEnabled(false);
        else
            loginBtn.setEnabled(true);
    }

    @OnClick(R.id.login)
    public void onClick(View view) {
        String mobile = mEtPhone.getText().toString();
        String passWord = mEtPwd.getText().toString();
        ScreenUtils.closeKeybord(mEtPhone,this);
        ScreenUtils.closeKeybord(mEtPwd,this);
        if(ValidateUtils.isMobileNO(mobile)) {
            if (cabinetDetailList == null)
                cabinetDetailList = JsonUtils.stringToObject(ShareManager.getValue(this, BaseConstant.LOCAL_CAB_LIST), ClassCabinetBean.class).getCabinetDetailList();

            String tip ="请输入本班教室的手机号码";
            for (CabinetBean one : cabinetDetailList) {
                if (one != null)
                    if (one.getMobile().equals(mobile)) {
                        if (one.getTeacherPassword().equals(MD5.md5(passWord))) {
                            Bundle bundle = new Bundle();
                            bundle.putString(ExtraName.KEY_DATA, one.getTeacherName());
                            IntentUtils.instance().startActivityFinish(this, EmergencyActivity.class, bundle);
                            return;
                        }else {
                            tip ="密码错误";
                        }
                    }
            }
            ToastView.showLong(tip);
        }else {
            ToastView.showLong("请输入正确的手机号码");
        }
    }

}
